﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace CNTVNow.Web.Helpers.Common
{
    public class Common
    {
        public static string GetPostedNews(DateTime datePosted)
        {
            if (datePosted == null)
                return DateTime.Now.ToString("dd/MM/yyyy");
            DateTime dtNow = DateTime.Now;
            if (datePosted.ToShortDateString() == dtNow.ToShortDateString())
            {
                int hoursNow = dtNow.Hour;
                int minutesNow = dtNow.Minute;
                int secondsNow = dtNow.Second;

                int hourPosted = datePosted.Hour;
                int minutePosted = datePosted.Minute;
                int secondePosted = datePosted.Second;

                if (hoursNow == hourPosted)
                {
                    if (minutesNow == minutePosted)
                    {
                        return string.Format(Resources.Common.DatePostedSecond, secondsNow - secondePosted);
                    }
                    else
                    {
                        return string.Format(Resources.Common.DatePostedMinute, minutesNow - minutePosted);
                    }
                }
                else
                {
                    return string.Format(Resources.Common.DatePostedHour, hoursNow - hourPosted);
                }
            }
            else
            {
                int daysNow = (dtNow.Month - FixCommon.BaseDate.Month) * FixCommon.DayInMonth + (dtNow.Day - FixCommon.BaseDate.Day);
                int daysPosted = (datePosted.Month - FixCommon.BaseDate.Month) * FixCommon.DayInMonth + (datePosted.Day - FixCommon.BaseDate.Day);
                int day = daysNow - daysPosted;
                return string.Format(Resources.Common.DatePostedDay, day);
            }
           return datePosted.ToString("dd/MM/yyyy");
        }
        public static string GetDatePosted(DateTime datePosted)
        {
            return datePosted.ToString("dd/MM/yyyy");
        }
        public static string GetTimePosted(DateTime datePosted)
        {
            return datePosted.ToString("hh:mm");
        }
        public static string GetCategoryUrl(int categoryId)
        {
            string Url = string.Empty;
            switch (categoryId)
            {
                case 2://News
                    Url = "News.cntv";
                    break;
                case 3://Sport
                    Url = "Sport.cntv";
                    break;
                case 4://Music
                    Url = "Music.cntv";
                    break;
                case 5://Movie
                    Url = "Movie.cntv";
                    break;
                case 6://Lifestyle
                    Url = "LifeStyle.cntv";
                    break;
                case 7://CNTV live
                    Url = "CNTVlive.cntv";
                    break;
            }
            return Url;
        }
        public static string GetDatePostedFull(DateTime datePosted)
        {
            return datePosted.ToString("dd/MM/yyyy hh:mm tt");
        }
        public static string GetTimePostedComment(DateTime datePosted)
        {
            int hour = DateTime.Now.Hour - datePosted.Hour;
            int day = DateTime.Now.Day - datePosted.Day;
            int month = DateTime.Now.Month - datePosted.Month;
            
            if(month< 0)
                month = 0;
            if(day < 0 )
                day = 0;
            hour = hour + (day * 24 + (month * 30));
            string result = string.Empty;
            if (hour > 0)
            {
                result = "about " + hour + " hour(s) ago";
            }
            else
            {
                result = "about " + (DateTime.Now.Minute - datePosted.Minute) +" minute(s) ago";
            }
            return result;
        }
        public static string GenerateRandomCode()
        {
            Random random = new Random();
            string s = "";
            for (int i = 0; i < 6; i++)
                s = String.Concat(s, random.Next(10).ToString());
            return s;
        }
    }
}